<?php 
class GoodsAttrControl extends CommonControl{
    /**
     * 商品属性列表页
     * @return [type] [description]
     */
     public function index()
     {
        $gt_id = q("gt_id", "intval"); //商品类型Id
        if ($gt_id) {
            $db = M("g_attr_class");
            $data = $db->where("goods_type_gt_id=$gt_id")->all();
            session("history", __URL__);
            $this->assign("field", $data);
            $this->display();
        }       
     }

     /**
      * 商品属性添加
      */
     public function add(){
        if (IS_POST) {
            $db = M('g_attr_class');
            $ac_id = $db->add();
            foreach ($_POST['attr_value'] as $k=>$v)
            {
                $_POST['attr_value'][$k]['attr_class_ac_id'] = $ac_id; 
            }

            foreach ($_POST['attr_value'] as $kc => $vc) 
            {
                $db->table("g_attr_value")->add($vc);
            }

            $this->success("添加属性成功", q("session.history"));
        } else {
            $this->display();
        }
     }

     /**
      * 商品属性删除
      * @return [type] [description]
      */
     public function del()
    {
        $ac_id = q("ac_id", "intval");
        if ($ac_id) {
            $db = M("g_attr_class");
            //删除属性
            $db->del($ac_id);
            $db->table("g_attr_value")->where("attr_class_ac_id=" . $ac_id)->del();
            $this->success("已删除记录", q("session.history"));
        }
    }

     /**
      * 商品编辑
      * @return [type] [description]
      */
    public function edit()
    {
        if (IS_POST) {
            //更新属性分类表
            $db = M("g_attr_class");
            $db->save();
            //更新属性值表
            $db = M("g_attr_value");
            //先移除属性
            $db->where("attr_class_ac_id=" . $_POST['ac_id'])->del();
            //添加属性使用replace into
            foreach ($_POST['attr_value'] as $d) {
                $d["attr_class_ac_id"] = $_POST['ac_id'];
                $db->replace($d);
            }
            $this->success("更新属性成功", session("history"));
        } else {
            $ac_id = q("ac_id", "intval");
            if ($ac_id) {
                //使用多表查询当前属性的所有值
                $db = K("AttrClassRelation");
                $field = $db->find($ac_id);
                $this->assign("field", $field);
                $this->display();
            }
        }
    }







}
 ?>